package com.rlax.ccdemo.service.user.fegin;

import com.google.common.collect.Lists;
import com.rlax.ccdemo.service.api.user.fegin.IAuthApi;
import com.rlax.ccdemo.service.user.entity.UpmsUser;
import com.rlax.ccdemo.service.user.service.UpmsUserService;
import com.rlax.corebin.core.result.R;
import com.rlax.corebin.security.entity.AuthUser;
import com.rlax.corebin.security.feign.AuthQuery;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

/**
 * 内部鉴权服务，feign、service调用方式，都通过该接口鉴权
 * @author Rlax
 *
 */
@RestController
@AllArgsConstructor
@Api(value = "内部鉴权服务", tags = "内部鉴权服务")
public class AuthApi implements IAuthApi {

    private final UpmsUserService upmsUserService;

    @Override
    public R<AuthUser> getAuthInfo(Long id, AuthQuery userQuery) {
        AuthUser authUser = new AuthUser();
        UpmsUser user = upmsUserService.getById(id);

        authUser.setUserId(user.getId());
        authUser.setLoginName(user.getUsername());
        authUser.setUserName(user.getRealname());
        if (userQuery.getRoles() || userQuery.getFull()) {
            if (user.getId() == 2) {
                authUser.setRoles(Lists.newArrayList("admin"));
            }
        }
        if (userQuery.getResource() || userQuery.getFull()) {
            if (user.getId() == 1) {
                authUser.setResources(Lists.newArrayList("user:detail", "product:detail"));
            }
        }
        return R.success(authUser);
    }

}
